package com.sy.ibuilds.pms.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

/**
 * 自定义登录逻辑
 */
@Service
public class UserDetailServiceImpl implements UserDetailsService {

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        System.out.println("执行了loadUserByUsername方法");
        //登录校验逻辑
        //1.查询数据库判断用户名是否存在，如果不存在抛出异常
        if (!"hzw".equals(username)){
            throw  new UsernameNotFoundException("用户名不存在！！！");
        }
        //2.把查询出来的密码进行解析，或者直接把密码放进构造方法
        String password = passwordEncoder.encode("hzw");

        return new User(username,password, AuthorityUtils
                .commaSeparatedStringToAuthorityList("admin,normal"));
    }
}
